
plot <- 
  estimation_data %>%
  select(A, validated_election_outcome, starts_with("avg")) %>%
  pivot_longer(avg_score_democ_support:avg_score_trust_elect) %>%
  filter(name %in% c("avg_score_democ_support", "avg_score_trust_elect")) %>%
  mutate(name = case_when(name == "avg_score_democ_support" ~ "Support for Democracy",
                          name == "avg_score_trust_elect" ~ "Trust in Elections")) %>%
  mutate(wave = paste0("Wave ", A)) %>%
  
  ggplot(aes(x=value, group = validated_election_outcome, fill = validated_election_outcome)) +
  geom_density(alpha = 1, adjust = 2) +
  facet_grid(name ~ wave, scales = "free", space = 'free') +
  theme_bw() + 
  ylab("Indexed Outcome") + 
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(), 
        panel.grid.major.y = element_line(size = .2),
        axis.title.x = element_blank()) + 
  theme(legend.title = element_blank()) + 
  theme(legend.position = "bottom") +
  theme(strip.background =element_rect(fill="white")) +
  theme(strip.text.y = element_text(angle = 90)) +
  scale_fill_manual(values = c("lightgrey", "darkgrey"))

ggsave(plot = plot, "./outputs/figures/figure_a1.pdf", width = 8, height = 5)
ggsave(plot = plot, "./outputs/figures/figure_a1.eps", device ="eps", width = 8, height = 5)
